import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { PublicCompoentsProvider } from '../../providers/public-compoents/public-compoents';
import { ApiProvider } from '../../providers/api/api'
import { Md5 } from 'ts-md5/dist/md5';
import { Storage } from '@ionic/storage';
import { UserBindBankPage } from "../user-bind-bank/user-bind-bank";


/**
 * Generated class for the UserCashPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
    selector: 'page-user-cash',
    templateUrl: 'user-cash.html',
})
export class UserCashPage {
    public money: any;
    public uid: any;
    public resData: any = {};
    public userMes: any = {};
    public myBankMes: any = {};
    public bankNum: any = {};
    public isBind: boolean = false;


    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public pubCom: PublicCompoentsProvider,
        public Api: ApiProvider,
        public storage: Storage) {

        storage.get('userId').then(
            (value: string) => {
                this.uid = value
            }
        );
    }

    ionViewDidEnter() {
        this.CheckBank();
        this.ChenkMes();
    }

    cash() {
        console.log(this.money)
        if (this.uid) {
            if (this.userMes.money >= this.money) {
                if (this.money >= Number(this.userMes.tx_min_price) && this.money <= Number(this.userMes.tx_max_price)) {
                    let timeStamp = (new Date()).getTime();
                    this.Api.cash({
                        times: timeStamp,
                        token: Md5.hashStr('HalfPriceMall' + timeStamp),
                        id: this.uid,
                        money: this.money
                    }).then(res => {
                        console.log(res)
                        this.resData = res;
                        if (this.resData.code == 1) {
                            this.pubCom.toast(this.resData.msg, 'middle');
                            this.ChenkMes();
                            this.money = '';
                        }
                    })
                } else {
                    this.pubCom.toast(`单笔提现最小${this.userMes.tx_min_price},最大${this.userMes.tx_max_price}`, 'bottom')
                }
            } else {
                this.pubCom.toast(`账户余额不足`, 'middle')
            }
        }
    };

    // 查看绑定的银行卡信息
    CheckBank() {
        let timeStamp = (new Date()).getTime();
        if (this.uid) {
            this.Api.isBindBank({
                times: timeStamp,
                token: Md5.hashStr('HalfPriceMall' + timeStamp),
                userid: this.uid
            }).then(res => {
                this.resData = res;
                if (this.resData.code == 1) {
                    if (this.resData.data.isblank != 0) {
                        // 有绑定银行卡信息，直接读取
                        this.isBind = true;
                        this.myBankMes = this.resData.data;
                        this.bankNum = this.myBankMes.blanknum.substr(-4, 4);
                    } else {
                        this.pubCom.toast('请先绑定银行卡', 'middle');
                        setTimeout(() => {
                            this.navCtrl.push(UserBindBankPage)
                        }, 1000);
                    }
                }
            })
        }
    };

    // 个人信息
    ChenkMes() {
        let timeStamp = (new Date()).getTime();
        if (this.uid) {
            this.Api.cashMes({
                times: timeStamp,
                token: Md5.hashStr('HalfPriceMall' + timeStamp),
                id: this.uid
            }).then(res => {
                console.log(res)
                this.resData = res;
                this.userMes = this.resData.data;
            });
        }
    }

}
